/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManagerModuleStaff.java
 *
 * Created on Dec 9, 2009, 9:36:21 AM
 */

package GUI;


import Bussiness.B_TableDataLoad;
import DatabaseAccess.D_DBHelper;
import java.awt.event.ItemEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 *
 * @author TienDung
 */
public class ManagerModuleStaff extends javax.swing.JPanel {

    /** Creates new form ManagerModuleStaff */
    private int dID=0;
    
    Vector v;
    private ResultSet rs;
    B_TableDataLoad tableload = null;
    D_DBHelper db = null;
    public ManagerModuleStaff() {
        db = new D_DBHelper();
        try {
            db.setConnectionUrl("connection.cfg");
        } catch (SQLException ex) {
            Logger.getLogger(AddNewFeedback.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(!db.setupConnection()){
            JOptionPane.showMessageDialog(null,"Can not open the connection","Connection Dialog",JOptionPane.ERROR_MESSAGE);
        }
        initComponents();
        setCombobox();
    }


    public void setCombobox(){
        try {
            v=new Vector();
            rs = db.executeProcedure("AllDepartment");
            while(rs.next()){
                cbbDepartment.addItem(rs.getString(2));
                v.add(rs.getInt(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManagerFeedbackDetail.class.getName()).log(Level.SEVERE, null, ex);
        }
        //cbbDepartment.setSelectedIndex(3);
    }

    public void AllStaff(int DID){
        Vector params = new Vector();
        params.add(DID);
        try {
            tableload = new B_TableDataLoad("connection.cfg");
            StaffList.setModel(tableload.getComplaintOfCustomer(StaffList, "DisplayAllStaff", params));
        } catch (Exception ex) {
            Logger.getLogger(ManagerModuleStaff.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        StaffList = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        cbbDepartment = new javax.swing.JComboBox();
        btnAddStaff = new javax.swing.JButton();
        btnStaffDetail = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Staff", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 3, 14))); // NOI18N

        StaffList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Staff Username", "Staff FullName"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        StaffList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                StaffListMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(StaffList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Department", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 3, 12))); // NOI18N

        cbbDepartment.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Department" }));
        cbbDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbDepartmentActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cbbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(cbbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAddStaff.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/Add.png"))); // NOI18N
        btnAddStaff.setText("Add ");

        btnStaffDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/information.png"))); // NOI18N
        btnStaffDetail.setText("Staff detail");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(447, 447, 447)
                        .addComponent(btnAddStaff, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnStaffDetail))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStaffDetail, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(btnAddStaff, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void StaffListMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StaffListMousePressed
        // TODO add your handling code here:
        StaffList.removeEditor();
        if(evt.getClickCount()>1){
            int iRow=this.StaffList.getSelectedRow();
            int SID=(Integer)tableload.ID.get(iRow);
            StaffDetail staffdetail = new StaffDetail(frmParent, true, SID);
            //System.out.println(tableload.ID.size());
            staffdetail.setVisible(true);
        }

    }//GEN-LAST:event_StaffListMousePressed

    private void cbbDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbDepartmentActionPerformed
        // TODO add your handling code here:
        Object o= null;
        for(int i=0;i<v.size();i++){
            if(cbbDepartment.getSelectedIndex()==i+1){
                o = v.elementAt(i);
            }else if(cbbDepartment.getSelectedIndex()==0){
                
            }
        }
        if(o!= null)
        {
            this.dID  = Integer.parseInt(o.toString().trim());
        }
        System.out.println(dID);
        AllStaff(dID);
    }//GEN-LAST:event_cbbDepartmentActionPerformed

private JFrame frmParent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable StaffList;
    private javax.swing.JButton btnAddStaff;
    private javax.swing.JButton btnStaffDetail;
    private javax.swing.JComboBox cbbDepartment;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
